#include "Texture.h"
#include "Global.h"

CTexture::CTexture(const CTexture &T)
{
	FileName = T.FileName;
	Size = T.Size;
	Cols = T.Cols;
	Rows = T.Rows;
	Count = T.Count;
	FrameWidth = T.FrameWidth;
	FrameHeight = T.FrameHeight;
	Load();
}

CTexture::CTexture(LPWSTR fileName, int cols, int rows, int count)
{
	Cols = cols;
	Rows = rows;
	Count = count;
	FileName = fileName;
	Load();
}

CTexture::~CTexture()
{
	if(this->Texture != NULL)
	{
		this->Texture->Release();
	}
}

void CTexture::Draw(int x, int y)
{
	D3DXVECTOR3 pos((float)x, (float)y, 0);
	G_SpriteHandler->Draw(Texture, &Size, NULL, &pos, 0xFFFFFFFF);
}

void CTexture::Load()
{
	D3DXIMAGE_INFO info;
	HRESULT hrs;

	hrs = D3DXGetImageInfoFromFile(FileName, &info);

	RECT s = {0, 0, info.Width, info.Height};
	Size = s;

	FrameWidth = info.Width;
	FrameHeight = info.Height;

	if(hrs != D3D_OK)
	{
		GLMessage(L"Can not load texture");
		return;
	}

	hrs = D3DXCreateTextureFromFileEx(
		G_Device,
		FileName,
		info.Width, 
		info.Height,
		1, 
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		0, //color
		&info,
		NULL,
		&Texture
		);

	if(hrs != D3D_OK)
	{
		GLMessage(L"Can not load texture");
		return;
	}
}